<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp" %>
<%
    String uuid = UUID.randomUUID().toString();
    request.setAttribute("uuid", uuid);
%>
<shiro:hasPermission name="QUERY_SEARCH_COL_MOD">
<a class="btn btn-primary" id="${uuid}_saveColForSearchBt">保存修改</a>
</shiro:hasPermission>

<shiro:hasPermission name="QUERY_SEARCH_COL_ADD">
<a class="btn btn-info" id="${uuid}_selectColForSearchBt">选择字段</a>
</shiro:hasPermission>

<div  id="${uuid}_searchColumnsBox" class="am-margin-top-sm">
	
</div>

<!-- 检索字段模板 -->
<textarea id='searchColumnDef_tpl' style='display:none;'>
	<form class="searchColDefItem">
	<table class="table table-bordered table-striped table-hover">
	<tr>
		<td colspan="2">
			<input type="hidden" name="id" value="<!:=id !>">
			<input type="hidden" name="queryDefId" value="<!:=queryDefId !>">
			<input type="hidden" name="name" value="<!:=name !>">
			<input type="hidden" name="columnName" value="<!:=columnName !>">
			<input type="hidden" name="tableName" value="<!:=tableName !>">
			<input type="hidden" name="exeScript" value="<!:=exeScript !>">
			<input type="hidden" name="inputValue" value="<!:=inputValue !>">
			<input type="hidden" name="controlScript" value="<!:=controlScript !>">
			<input type="hidden" name="columnType" value="<!:=columnType !>">
			
			<input type="text" style="width:50px" name="prefix" value="<!:=prefix !>" placeholder="前缀">
			.
			<!:=columnName !>
		</td>
		<td>
			<a class="btn btn-link btn-sm _j_upBt">
				<span class="glyphicon glyphicon-chevron-up"></span>
			</a>
			<a class="btn btn-link btn-sm _j_downBt">
				<span class="glyphicon glyphicon glyphicon-chevron-down"></span>
			</a>
			<shiro:hasPermission name="QUERY_SEARCH_COL_DEL">
			<a class="btn btn-link btn-sm _j_removeBt">
				<span class="glyphicon glyphicon glyphicon-trash"></span>
			</a>
			</shiro:hasPermission>
		</td>
	</tr>
	<tr>
		<td width="220px">
			<div>
			<a style="width: 80px;display:inline-block">显示名</a>
			<input type="text" style="width: 90px" value="<!:=viewName!>" name="viewName">
			</div>
			
			<div class="am-margin-top-sm">
			<a style="width: 80px;display:inline-block">输入方式</a>
			<select name="inputWap">
				<option value="1" selected="selected">用户输入</option>
				<option value="2">参数传入</option>
				<option value="3">固定值</option>
			</select>
			</div>
		</td>
		<td>
			<div>
				<a style="width: 80px;display:inline-block">条件</a>
				<select name="conditionType">
					<option value="3" selected="selected">等于</option>
					<option value="2">包含</option>
					<option value="4">不等于</option>
					<option value="5">左匹配</option>
					<option value="6">右匹配</option>
					<option value="7">小于</option>
					<option value="8">小于等于</option>
					<option value="9">大于</option>
					<option value="10">大于等于</option>
					<option value="11">时间段</option>
				</select>
			</div>
			
			
			<div class="inputWap inputWap_1">
				<div class="am-margin-top-sm">
					<a style="width: 80px;display:inline-block">控件类型</a>
					<select name="controlType">
						<option value="1" selected="selected">文本框</option>
						<option value="2">日期</option>
						<option value="3">时间</option>
						<option value="4">字典</option>
					</select>
				</div>
				
				<div style="display: none;" class="am-margin-top-sm controlType controlType_4">
					<a style="width: 80px;display:inline-block">字典值</a>
					<input type="text" style="width: 90px" name="controlType_4">
				</div>
			</div>
			
			<div class="am-margin-top-sm inputWap inputWap_3">
				<a style="width: 80px;display:inline-block">固定值</a>
				<input type="text" style="width: 90px" name="inputWap_3">
			</div>
			
		</td>
		<td>
			<input type="checkbox" value="1" name="canView">是否显示 <br/>
			<input type="checkbox" value="1" name="asScriptCal">仅用于脚本
		</td>
	</tr>
	</table>
	</form>
</textarea>
<script type="text/javascript">
var queryDefId='${param.queryDefId}';
$(function(){
	var uuid = '${uuid}';
	
	var searchColumnDef_tpl=baidu.template("searchColumnDef_tpl");
	var searchColumnsBox=$("#" + uuid + "_searchColumnsBox");
	

	var _saveColForSearchBt=$("#" + uuid + "_saveColForSearchBt").click(function(){
		webUtils.alert("正在保存...");
		if(queryDefId){
			var datas=[];
			searchColumnsBox.find(".searchColDefItem").each(function(i,n){
				var data = webUtils.formToObj($(n));
				
				if(data.controlType==4){
					data.controlScript=data["controlType_"+data.controlType];
				}
				data.inputValue=data["inputWap_"+data.inputWap];
				datas.push(data);
			});
			$.post(ctx+"/queryDefAction/saveSearchColDefs.do",{datas:JSON.stringify(datas)},function(res){
				if(!res.success){
					webUtils.alert(res.msg);
				}else{
					webUtils.alert("保存成功!");
					loadSearchColumnDefs();
				}
			},"json");
		}
	});
	
	var _selectColForSearchBt=$("#" + uuid + "_selectColForSearchBt").click(function(){
		if(!queryDefId){
			webUtils.alert("请选择左手边具体的查询定义");
			return false;
		}
		var sub = top.webUtils.topicSubscribe("cols.select.success", function (args) {
			columnsDialog.close();
			
			$.each(args,function(i,n){
				var d={
						name:n.label,
						viewName:n.comment?n.comment:n.label,
						width:'auto',
						prefix:'',
						columnName:n.name,
						tableName:n.tableName,
						canView:1,
						asScriptCal:0,
						conditionType:1,
						inputWap:1,
						inputValue:'',
						controlType:'',
						exeScript:'',
						controlScript:'',
						columnType:n.columnType,
						queryDefId:queryDefId
				}
				d.name=d.name.toUpperCase();
				if(searchColumnsBox.find("input[name=name][value="+d.name+"]").length<1){
					var $searchColumnDef=renderSearchColumnDef($(searchColumnDef_tpl(d)),d);
					searchColumnsBox.append($searchColumnDef);
				}
			});
        });
		
		var columnsDialog=selectColumn({
			onClose:function(){
				sub.remove();
			}
		});
	});
	
	function loadSearchColumnDefs(){
		$.getJSON(ctx+"/queryDefAction/loadSearchColDefs.do",{id:queryDefId},function(res){
			searchColumnsBox.html("");
			$.each(res,function(i,n){
				if(searchColumnsBox.find("input[name=name][value="+n.name+"]").length<1){
					var $searchColumnDef=renderSearchColumnDef($(searchColumnDef_tpl(n)),n);
					searchColumnsBox.append($searchColumnDef);
				}
			}) 
		});
	}
	

	function renderSearchColumnDef($searchColumnDef,data){
		$searchColumnDef.find("._j_removeBt").click(function(){
			var id=$searchColumnDef.find("input[name=id]").val();
			if(id){
				$.post(ctx+"/queryDefAction/removeSearchColDef.do",{id:id},function(res){
					$searchColumnDef.remove();
					webUtils.alert("删除成功!");
				},"json");
			}else{
				$searchColumnDef.remove();
				webUtils.alert("删除成功!");
			}
		});
		$searchColumnDef.find("._j_downBt").click(function(){
			var next=$searchColumnDef.next();
			if(next.length>0){
				$searchColumnDef.insertAfter(next);
			}
		});
		$searchColumnDef.find("._j_upBt").click(function(){
			var prev=$searchColumnDef.prev();
			if(prev.length>0){
				$searchColumnDef.insertBefore(prev);
			}
		});
		
		$searchColumnDef.find("select[name=controlType]").change(function(){
			$searchColumnDef.find(".controlType").hide();
			$searchColumnDef.find(".controlType_"+$(this).val()).show();
		});
		
		$searchColumnDef.find("select[name=inputWap]").change(function(){
			$searchColumnDef.find(".inputWap").hide();
			$searchColumnDef.find(".inputWap_"+$(this).val()).show();
		});
		
		if(data){
			$searchColumnDef.find(".controlType").hide();
			$searchColumnDef.find(".controlType_"+data.controlType).show();
			
			$searchColumnDef.find(".inputWap").hide();
			$searchColumnDef.find(".inputWap_"+data.inputWap).show();
			
			if(data.controlType!=0){
				data["controlType_"+data.controlType]=data.controlScript;
			}
			
			data["inputWap_"+data.inputWap]=data.inputValue;
			
			webUtils.fullForm({
				form:$searchColumnDef,
				data:data
			});
			
			
		}
		
		return $searchColumnDef;
	}

	loadSearchColumnDefs();
});
</script>
